package com.onluy.framework.data.util;


import java.util.Comparator;

public class DataComparator
  implements Comparator
{
  private String key;
  private int keyType;
  private int order;

  public DataComparator(String key, int keyType, int order)
  {
    this.key = key;
    this.keyType = keyType;
    this.order = order;
  }

  public int compare(Object o1, Object o2) {
    IData data1 = (IData)o1;
    IData data2 = (IData)o2;

    if (this.order == 0) {
      if (this.keyType == 2) {
        String value1 = data1.getString(this.key);
        String value2 = data2.getString(this.key);
        return value1.compareTo(value2);
      }if (this.keyType == 3) {
        int value1 = data1.getInt(this.key);
        int value2 = data2.getInt(this.key);
        return value1 == value2 ? 0 : value1 < value2 ? -1 : 1;
      }if (this.keyType == 4) {
        double value1 = data1.getDouble(this.key);
        double value2 = data2.getDouble(this.key);
        return value1 == value2 ? 0 : value1 < value2 ? -1 : 1;
      }
    } else {
      if (this.keyType == 2) {
        String value1 = data1.getString(this.key);
        String value2 = data2.getString(this.key);
        return value2.compareTo(value1);
      }if (this.keyType == 3) {
        int value1 = data1.getInt(this.key);
        int value2 = data2.getInt(this.key);
        return value1 == value2 ? 0 : value1 > value2 ? -1 : 1;
      }if (this.keyType == 4) {
        double value1 = data1.getDouble(this.key);
        double value2 = data2.getDouble(this.key);
        return value1 == value2 ? 0 : value1 > value2 ? -1 : 1;
      }
    }
    return 0;
  }
}